package org.dreamwork.jasmine2.events;

import org.dreamwork.jasmine2.engine.IChangable;
import org.dreamwork.jasmine2.web.IWebControl;

import java.util.List;
import java.util.ArrayList;
import java.util.Vector;

/**
 * Created by IntelliJ IDEA.
 * User: seth
 * Date: 2009-7-23
 * Time: 11:56:36
 */
public class ChangableDelegate implements IChangable {
    private final Vector<IChangeListener> changeListeners = new Vector<IChangeListener> ();
    public synchronized void addChangeListener (IChangeListener listener) {
        changeListeners.add (listener);
    }

    public synchronized void removeChangeListener (IChangeListener listener) {
        changeListeners.removeElement (listener);
    }

    public void fireChange (IWebControl sender, String attrName, Object oldValue, Object newValue) throws EventException {
        IChangeListener[] listeners = getChangeListeners ();
        for (IChangeListener listener : listeners)
            listener.onChange (sender, attrName, oldValue, newValue);
    }

    public IChangeListener[] getChangeListeners () {
        List<IChangeListener> list = new ArrayList<IChangeListener> ();
        synchronized (changeListeners) {
            list.addAll (changeListeners);
        }
        IChangeListener[] array = new IChangeListener[list.size ()];
        return list.toArray (array);
    }
}